/**
 * 
 */
package org.swxjava.util;

import java.io.UnsupportedEncodingException;

import org.swxjava.assembler.Constants;

public class StringUtils {

	private static Log LOG = LogFactory.getLog(StringUtils.class);

	public static boolean isBlank(Object str) {
		if (str == null) {
			return true;
		}
		String value = String.valueOf(str).replaceAll(" ", "");
		if ("".equals(value)) {
			return true;
		} else {
			return false;
		}
	}

	public static String stringToHex(String str) {
		if (str == null) {
			// FIXME
			return Constants.NULL_TERMINATOR;
		}
		String result = null;
		try {
			byte[] strarray = str.getBytes("UTF-8");
			result = convertBytesToHexString(strarray);
		} catch (UnsupportedEncodingException e) {
			LOG.error(e);
		}
		return result;
	}

	public static String convertBytesToHexString(byte[] bytes) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < bytes.length; i++) {
			String hs = Integer.toHexString(bytes[i]);
			if (hs != null && hs.length() > 2) {
				hs = hs.substring(hs.length() - 2, hs.length());
			} else if (hs != null && hs.length() == 1) {
				hs = "0" + hs;
			}
			sb.append(hs);
		}
		return sb.toString();
	}

	public static String hexToString(String hexstr) {
		byte[] byteArray = convertHexStringToBytes(hexstr);
		return getUTF8String(byteArray, byteArray.length);
	}

	public static byte[] convertHexStringToBytes(String hexstr) {
		hexstr = hexstr.replace(" ", "");
		String[] hexArray = split(hexstr, 2);
		byte[] byteArray = new byte[hexArray.length];
		for (int i = 0; i < hexArray.length; i++) {
			byte b = (byte) Integer.parseInt(hexArray[i], 16);
			// LOG.debug(ch + " -- > " + b);
			byteArray[i] = b;
		}
		return byteArray;
	}

	public static String[] split(String str, int splitLength) {
		int size = str.length() / splitLength;
		String[] results = new String[size];
		for (int i = 0; i < results.length; i++) {
			StringBuffer sb = new StringBuffer();
			for (int j = 0; j < splitLength; j++) {
				sb.append(str.charAt((i * splitLength) + j));
			}
			results[i] = sb.toString();
		}
		return results;
	}

	public static String join(Object[] array) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < array.length; i++) {
			sb.append(array[i]);
		}
		return sb.toString();
	}

	public static final String getUTF8String(final byte[] bytearr,
			final int utflen) {
		try {
			return new String(bytearr, 0, utflen, "UTF-8");
		} catch (final UnsupportedEncodingException e) {
			return null;
		}
	}

	public static String getStringLengthInBytesHex(String strInHex, int numBytes) {
		// Divide length in chars by 2 to get length in bytes
		int bytecodeLenInDec = strInHex.length() / 2;
		String bytecodeLenInHex = getIntAsHex(bytecodeLenInDec, numBytes);
		return bytecodeLenInHex;
	}

	public static String getIntAsHex(int integer, int numBytes) {
		String intAsHex = paddingWithZero(integer, numBytes * 2);
		intAsHex = makeLittleEndian(intAsHex);
		return intAsHex;
	}

	public static String paddingWithZero(Object val, int digit) {
		// only works in JDK5
		return String.format("%0" + digit + "X", val);
	}

	public static String makeLittleEndian(String str) {
		int sLen = str.length();
		// Make sure that the string is padded to the byte boundary
		if (sLen % 2 == 1) {
			sLen++;
			str = "0" + str;
		}
		String[] strArr = split(str, 2);
		ArrayUtils.reverse(strArr);
		String strLittleEndian = join(strArr);
		return strLittleEndian;
	}

	public static String reverseHexString(String hex) {
		String[] bytes = split(hex, 2);
		ArrayUtils.reverse(bytes);
		return join(bytes);
	}
}
